<?php

$offset = isset($_GET['off']) ? (int)$_GET['off'] : 0 ;

$sql = "SELECT COUNT(*) AS cnt FROM ".TABLE_AUTO_UPDATE_LOG." WHERE error_type != 'fatal'";

$query = $db->execute($sql);

$maxrecords = $query->fields['cnt'] ;

$sql = "SELECT * FROM ".TABLE_AUTO_UPDATE_LOG." WHERE error_type != 'fatal' ORDER BY id DESC LIMIT ".$offset.",21";

$query = $db->execute($sql);

if($maxrecords>20){
  $pager = 'Page: ';
  if($offset>0){
    $pager .= '<a href="'.zen_href_link(FILENAME_AUTO_UPDATE, 'page=logs&off='.($offset-20).'#autolog').'">'.PREVNEXT_BUTTON_PREV.' '.TEXT_PREVIOUS.'</a> ';
  }
  for($i=0; $i<$maxrecords; $i+=20){
    $style = ($i==$offset) ? ' style="font-weight:bold; text-decoration: underline" ' : '';
    $pager .= '<a href="'.zen_href_link(FILENAME_AUTO_UPDATE, 'page=logs&off='.$i.'#autolog').'"'.$style.'>'.ceil(($i+1)/20).'</a> ';
  }
  if($query->recordcount()>20){
    $pager .= '<a href="'.zen_href_link(FILENAME_AUTO_UPDATE, 'page=logs&off='.($offset+20).'#autolog').'">'.PREVNEXT_BUTTON_NEXT.' '.TEXT_NEXT.'</a>';
  }
}else{
  $pager = 'Page: 1';
}


?>
<a name="autolog"></a>
<h2><?php echo HEADING_LOG_TABLE ; ?> <?php echo $offset ?>, <?php echo ($offset+20) ?></h2>

<table class="su_admin_table">
  <tr>
      <td colspan="6"><?php echo $pager ; ?></td>
  </tr>
  <tr>
  <th><?php echo TABLE_HEADING_LOGGED ; ?></th>
  <th><?php echo TABLE_HEADING_NEW_MODULE ; ?></th>
  <th><?php echo TABLE_HEADING_NEW_VERSION ; ?></th>
  <th><?php echo TABLE_HEADING_OLD_MODULE ; ?></th>
  <th><?php echo TABLE_HEADING_OLD_VERSION ; ?></th>
  <th><?php echo TABLE_HEADING_MESSAGE ; ?></th>
  </tr>
<?php

for($i=0; $i<($maxrecords-1) && $i<20; $i++){
// print_r($query->fields); print '<br />';
print '<tr><td>'.$query->fields['logged'].'</td><td>'.$query->fields['module'].'</td><td class="cntr">'.$query->fields['version'].'</td><td>'.$query->fields['replacing_module'].'</td><td class="cntr">'.$query->fields['replacing_version'].'</td><td>'.$query->fields['message'].'</td></tr>';
$query->MoveNext();
}

?>
</table>

<?php
  if(sizeof($error_recovery)>0){
?>
  <h2>Re-attempt auto-update.</h2>
  <p>You can attempt to re-install modules that have failed to install by clicking on the module name below. This will re-schedule the module for download and installation. Failure may be due to a download error or the update server may have been busy. Installation will only happen is an updated module is confirmed as a cleanly downloaded. If a module detects that changes have been made to core ShopUpgraded files that it may overwrite, then the module will not install and will leave it's files for you to manually merge or move into place.<br /><br />
<?php
    echo join('<br />', $error_recovery );
  }
  echo '</p>';


$erroffset = isset($_GET['erroff']) ? (int)$_GET['erroff'] : 0 ;

$sql = "SELECT COUNT(*) AS cnt FROM ".TABLE_AUTO_UPDATE_LOG." WHERE error_type = 'fatal'";

$query = $db->execute($sql);

$maxrecords = $query->fields['cnt'] ;

$sql = "SELECT * FROM ".TABLE_AUTO_UPDATE_LOG." WHERE error_type = 'fatal' ORDER BY id DESC LIMIT ".$erroffset.",21";

$query = $db->execute($sql);

if($maxrecords>20){
  $pager = 'Page: ';
  if($erroffset>0){
    $pager .= '<a href="'.zen_href_link(FILENAME_AUTO_UPDATE, 'page=logs&erroff='.($erroffset-20).'#autoerrlog').'">'.PREVNEXT_BUTTON_PREV.' '.TEXT_PREVIOUS.'</a> ';
  }
  for($i=0; $i<$maxrecords; $i+=20){
    $style = ($i==$erroffset) ? ' style="font-weight:bold; text-decoration: underline" ' : '';
    $pager .= '<a href="'.zen_href_link(FILENAME_AUTO_UPDATE, 'page=logs&erroff='.$i.'#autoerrlog').'"'.$style.'>'.ceil(($i+1)/20).'</a> ';
  }
  if($query->recordcount()>20){
    $pager .= '<a href="'.zen_href_link(FILENAME_AUTO_UPDATE, 'page=logs&erroff='.($erroffset+20).'#autoerrlog').'">'.PREVNEXT_BUTTON_NEXT.' '.TEXT_NEXT.'</a>';
  }
}else{
  $pager = 'Page: 1';
}


?>

<a name="autoerrlog"></a>

<h2><?php echo HEADING_ERROR_TABLE ; ?> <?php echo $erroffset ?>, <?php echo ($erroffset+20) ?></h2>

<table class="su_admin_table">
  <tr>
      <td colspan="6"><?php echo $pager ; ?></td>
  </tr>
  <tr>
  <th><?php echo TABLE_HEADING_LOGGED ; ?></th>
  <th><?php echo TABLE_HEADING_NEW_MODULE ; ?></th>
  <th><?php echo TABLE_HEADING_NEW_VERSION ; ?></th>
  <th><?php echo TABLE_HEADING_OLD_MODULE ; ?></th>
  <th><?php echo TABLE_HEADING_OLD_VERSION ; ?></th>
  <th><?php echo TABLE_HEADING_MESSAGE ; ?></th>
  </tr>
<?php

for($i=0; $i<($maxrecords-1) && $i<20 ; $i++){
// print_r($query->fields); print '<br />';
print '<tr><td>'.$query->fields['logged'].'</td><td>'.$query->fields['module'].'</td><td class="cntr">'.$query->fields['version'].'</td><td>'.$query->fields['replacing_module'].'</td><td class="cntr">'.$query->fields['replacing_version'].'</td><td>'.$query->fields['message'].'</td></tr>';
$query->MoveNext();
}

?>
</table>